package top.moonkit.rs_pro.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import top.moonkit.rs_pro.entity.LoginCheckCode;
import top.moonkit.rs_pro.mapper.LoginCheckCodeMapper;
import top.moonkit.rs_pro.service.LoginCheckCodeService;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;

import static top.moonkit.rs_pro.entity.table.LoginCheckCodeTableDef.LOGIN_CHECK_CODE;

/**
 * 登录验证码 服务层实现。
 *
 * @author moon
 * @since 0.0.1
 */
@Service
public class LoginCheckCodeServiceImpl extends ServiceImpl<LoginCheckCodeMapper, LoginCheckCode>  implements LoginCheckCodeService{

    @Override
    public boolean validateCode(String account, String code) {
        // 查找有效的验证码
        LoginCheckCode checkCode = this.getOne(
                QueryWrapper.create()
                        .and(LOGIN_CHECK_CODE.USER_ACCOUNT.eq(account))
                        .and(LOGIN_CHECK_CODE.CODE.eq(code))
                        .and(LOGIN_CHECK_CODE.EXPIRE_TIME.ge(Timestamp.valueOf(LocalDateTime.now())))
        );

        return checkCode != null;
    }
}
